package com.imooc.reader.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.imooc.reader.management.entity.Role;
import com.imooc.reader.management.entity.RoleUser;
import com.imooc.reader.management.entity.User;
import com.imooc.reader.management.mapper.RoleMapper;
import com.imooc.reader.management.mapper.RoleUserMapper;
import com.imooc.reader.management.mapper.UserMapper;
import com.imooc.reader.management.service.UserService;
import com.imooc.reader.management.service.impl.exception.BusinessException;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleUserMapper roleUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        if(user != null){
            return user;
        }else{
            throw new UsernameNotFoundException("INCORRECT_USERNAME_OR_PASSWORD");
        }
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public User createUser(String username, String password, String empName, String deptName) {
        QueryWrapper<User> condition = new QueryWrapper<>();
        condition.eq("username", username);
        List<User> users = userMapper.selectList(condition);
        if(users.size() > 0){
            throw new BusinessException("USERNAME_ALREADY_EXISTS", "用户名已存在");
        }
        User user = new User(username, new BCryptPasswordEncoder().encode(password), 1, empName, deptName);
        userMapper.insert(user);
        QueryWrapper<Role> roleCondition = new QueryWrapper<>();
        roleCondition.eq("role_code", "USER");
        Role role = roleMapper.selectOne(roleCondition);
        roleUserMapper.insert(new RoleUser(role.getRoleId(), user.getUserId()));
        return user;
    }

    @Override
    public IPage<User> selectByRoleCode(IPage page, String roleCode) {
        return userMapper.selectByRoleCode(page,roleCode);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public User updateBasicInfo(Integer userId, String empName, String deptName) {
        User user = userMapper.selectById(userId);
        user.setEmpName(empName);
        user.setDeptName(deptName);
        userMapper.updateById(user);
        return user;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public User updateStatus(Integer userId) {
        User user = userMapper.selectById(userId);
        user.setStatus(user.getStatus()==1?0:1);
        userMapper.updateById(user);
        return user;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public User deleteUser(Integer userId) {
        User user = userMapper.selectById(userId);
        userMapper.deleteById(userId);
        QueryWrapper<RoleUser> condition = new QueryWrapper<>();
        condition.eq("user_id", userId);
        roleUserMapper.delete(condition);
        return user;
    }
}
